01 - ggplot2 theme
The aim of the themes is to help giving an “IMPACT” touch to the graphs, not to create them. The graph should already be created, i.e. type of plots, and the correct aesthetics, labeling, etc.
The following example aims to plot the percentages of households by category of water sources, by gender of the head of households (This a dummy dataset).
Barplot without formating
data_to_plot <- presentresults::presentresults_MSNA2024_labelled_results_table |>
dplyr::filter(
analysis_var == "wash_drinking_water_source_cat",
group_var == "hoh_gender",
group_var_value %in% c("male", "female")
) |>
dplyr::mutate(label_analysis_var_value = factor(label_analysis_var_value,
levels = c("Improved",
"Unimproved",
"Surface water",
"Undefined")))
initialplot <- data_to_plot %>%
ggplot2::ggplot() +
ggplot2::geom_col(
ggplot2::aes(
x = label_analysis_var_value,
y = stat,
fill = label_group_var_value
),
position = "dodge"
) +
ggplot2::labs(
title = stringr::str_wrap(unique(data_to_plot$indicator), 50),
x = stringr::str_wrap(unique(data_to_plot$label_analysis_var), 50),
fill = stringr::str_wrap(unique(data_to_plot$label_group_var), 20)
)initialplottheme_barplot
theme_barplot will give REACH color palette to the bar plot, put the y-axis to 0 to 100%.
initialplot +
theme_barplot()theme_barplottheme_impact
theme_impact will change the background and color of the title.
initialplot +
theme_barplot() +
theme_impact("reach")theme_barplot and theme_impactOther palettes
initialplot +
theme_barplot(palette = impact_palettes$impact_palette) +
theme_impact("impact")theme_barplot and theme_impact set with IMPACT themeSome palettes are available in the impact_palettes object.
impact_palettes$reach_palette
[1] "#58585A" "#EE5859" "#D2CBB8" "#c7c8ca"
$impact_palette
[1] "#000000" "#315975" "#58585A"
$agora_palette
[1] "#581522" "#023C40" "#4F9C35" "#F56741" "#F6ECD0"
$tol_palette
[1] "#322288" "#505050" "#44AA99" "#88CCEE" "#DDCC77" "#EE5859" "#AA4499"
[8] "#721621"
$high_contrast_tol_palette
[1] "#275587" "#CEA936" "#A35464"
$wong_palette
[1] "#EE5859" "#E69F00" "#56B4E9" "#322288" "#F0E442" "#0072B2" "#505050"
[8] "#CC79A7"
$divergent
[1] "#F15B22" "#F58120" "#FBAB35" "#209EA0" "#008083" "#0072B2" "#016060"
$divergent_with_neutral
[1] "#721621" "#D7191C" "#FDAE61" "#FFFFBF" "#97D3C3" "#209EA0" "#016060"
Errors and number of colors
The palette should have enough color to match the scale fill. The impact palette only has 3 colors while the graph needs 4.
data_to_plot <- presentresults::presentresults_MSNA2024_labelled_results_table |>
dplyr::filter(
analysis_var == "snfi_fds_cannot_cat",
group_var == "hoh_gender")
initialplot <- data_to_plot %>%
ggplot2::ggplot() +
ggplot2::geom_col(
ggplot2::aes(
x = label_analysis_var_value,
y = stat,
fill = label_group_var_value
),
position = "dodge"
) +
ggplot2::labs(
title = stringr::str_wrap(unique(data_to_plot$indicator), 50),
x = stringr::str_wrap(unique(data_to_plot$label_analysis_var), 50),
fill = stringr::str_wrap(unique(data_to_plot$label_group_var), 20)
)
initialplot +
theme_barplot(palette = impact_palettes$impact_palette) +
theme_impact("impact")Error in `palette()`:
! Insufficient values in manual scale. 4 needed but only 3 provided.
If you need more colors, have a look at the grDevices::colorRampPalette
function_couleur <- grDevices::colorRampPalette(impact_palettes$divergent_with_neutral)
function_couleur(20) [1] "#721621" "#91161F" "#B1171D" "#D1181C" "#E1402E" "#ED6F43" "#F99E59"
[8] "#FDBF74" "#FED892" "#FEF2B0" "#EEF8BF" "#CDEAC0" "#ACDCC2" "#8ACDBF"
[15] "#64BCB4" "#3FABA9" "#1E9A9C" "#148788" "#0A7374" "#016060"
This section comes from this vignette.